Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Namespace BMLERP
	Friend Class frmACP100
		Inherits System.Windows.Forms.Form
		Dim OldVMVDN As String
		Private Sub WriteToFile()
			Dim SQLInsert As String
			Dim LastDate As Object
			LastDate = VB6.Format(Now, "yyyy-mm-dd hh:mm:ss")

			If ISADDING Then
				SQLInsert = "INSERT INTO AVM " & "(VMVDN,VMNME,VMADD" & ",VMTEL,VMFAX" & ",VMTYP,VMTPM" & ",VMYPA,VMDLV" & ",VMRID,VMLDT,VMLUS) " & " VALUES " & "('" & txtVMVDN.Text & "', '" & txtVMNME.Text & "','" & txtVMADD.Text & "'" & ",'" & txtVMTEL.Text & "','" & txtVMFax.Text & "'" & ",'" & VB.Left(VB6.GetItemString(cboVMTYP, cboVMTYP.SelectedIndex), 1) & "','" & txtVMTPM.Text & "'" & "," & txtVMYPA.Text & ",'" & VB.Left(VB6.GetItemString(cboVMDLV, cboVMDLV.SelectedIndex), 1) & "'" & ",'VM','" & LastDate & "','" & UserLogin & "')"
			Else
				SQLInsert = "UPDATE AVM SET VMVDN = '" & txtVMVDN.Text & "', VMNME ='" & txtVMNME.Text & "',VMADD='" & txtVMADD.Text & "'" & ",VMTEL='" & txtVMTEL.Text & "',VMFax ='" & txtVMFax.Text & "'" & ",VMTYP='" & VB.Left(VB6.GetItemString(cboVMTYP, cboVMTYP.SelectedIndex), 1) & "',VMTPM='" & txtVMTPM.Text & "'" & ",VMYPA=" & txtVMYPA.Text & ",VMDLV='" & VB.Left(VB6.GetItemString(cboVMDLV, cboVMDLV.SelectedIndex), 1) & "'" & ",VMLDT='" & LastDate & "',VMLUS='" & UserLogin & "' " & " WHERE VMVDN ='" & OldVMVDN & "'"
			End If
			MsgBox(SQLInsert)
			DBconn.Execute(SQLInsert)
		End Sub

		Private Sub WriteToScreen()
			Dim i As Object
			Dim TBL As ADODB.Recordset
			If ISADDING Then 'gan rong
				cboVMVDN.BoundText = ""
				txtVMVDN.Text = ""
				txtVMNME.Text = ""
				txtVMADD.Text = ""
				txtVMTEL.Text = ""
				txtVMFax.Text = ""
				cboVMTYP.SelectedIndex = -1
				txtVMTPM.Text = ""
				txtPTDSC.Text = ""
				txtVMYPA.Text = CStr(0)
				cboVMDLV.SelectedIndex = -1
			Else
				TBL = New ADODB.Recordset
				TBL.Open("  Select AVM.* From AVM where  VMVDN ='" & cboVMVDN.BoundText & "'", DBconn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, ADODB.CommandTypeEnum.adCmdText)
				If TBL.EOF = False Then
					txtVMVDN.Text = IIf(IsDBNull(TBL.Fields("VMVDN").Value), "", TBL.Fields("VMVDN").Value)
					cboVMVDN.BoundText = txtVMVDN.Text
					txtVMNME.Text = IIf(IsDBNull(TBL.Fields("VMNME").Value), "", TBL.Fields("VMNME").Value)
					txtVMADD.Text = IIf(IsDBNull(TBL.Fields("VMADD").Value), "", TBL.Fields("VMADD").Value)
					txtVMTEL.Text = IIf(IsDBNull(TBL.Fields("VMTEL").Value), "", TBL.Fields("VMTEL").Value)
					txtVMFax.Text = IIf(IsDBNull(TBL.Fields("VMFAX").Value), "", TBL.Fields("VMFAX").Value)
					'cboVMTYP = IIf(IsNull(TBL!VMTYP), "", TBL!VMTYP)
					If Not IsDBNull(TBL.Fields("VMTYP").Value) Then
						For i = 0 To cboVMTYP.Items.Count - 1
							If VB.Left(VB6.GetItemString(cboVMTYP, i), 1) = TBL.Fields("VMTYP").Value Then
								cboVMTYP.SelectedIndex = i
								Exit For
							End If
						Next i
					Else
						cboVMTYP.SelectedIndex = -1
					End If
					txtVMTPM.Text = IIf(IsDBNull(TBL.Fields("VMTPM").Value), "", TBL.Fields("VMTPM").Value)
					txtPTDSC.Text = Find("APT", "PTDSC", " WHERE PTPTC='" & txtVMTPM.Text & "'")
					txtVMYPA.Text = IIf(IsDBNull(TBL.Fields("VMYPA").Value), 0, TBL.Fields("VMYPA").Value)
					'cboVMDLV = IIf(IsNull(TBL!VMDLV), "", TBL!VMDLV)
					If Not IsDBNull(TBL.Fields("VMDLV").Value) Then
						For i = 0 To cboVMDLV.Items.Count - 1
							If VB.Left(VB6.GetItemString(cboVMDLV, i), 1) = TBL.Fields("VMDLV").Value Then
								cboVMDLV.SelectedIndex = i
								Exit For
							End If
						Next i
					Else
						cboVMDLV.SelectedIndex = -1
					End If
				Else
					cboVMVDN.BoundText = ""
					txtVMVDN.Text = ""
					txtVMNME.Text = ""
					txtVMADD.Text = ""
					txtVMTEL.Text = ""
					txtVMFax.Text = ""
					cboVMTYP.SelectedIndex = -1
					txtVMTPM.Text = ""
					txtVMYPA.Text = CStr(0)
					cboVMDLV.SelectedIndex = -1
				End If
				TBL.Close()
			End If
			OldVMVDN = txtVMVDN.Text
		End Sub

		Sub ButtonStatus()
			cmdAddnew.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING	' And txtIMITN <> ""
			cmdDelete.Enabled = Not ISEDITING 'And txtIMITN <> ""
			cmdCancel.Enabled = ISEDITING
			Frame1.Enabled = ISEDITING
			'txtIMITN.Locked = Not ISADDING ' Lock item code tru truong hop ISADDING=true
			'txtDepartment.Enabled = ISEDITING
			'cboDepartment.Visible = Not ISADDING
			'txtDepartmentID.Visible = ISADDING
		End Sub



		Private Sub cboVMTPM_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboVMTPM.ClickEvent
			If eventArgs.area = 2 Then
				txtVMTPM.Text = cboVMTPM.BoundText
				txtPTDSC.Text = cboVMTPM.CtlText
				cboVMTPM.Visible = False
				cboVMDLV.Focus()
			End If
		End Sub

		Private Sub cboVMTYP_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboVMTYP.SelectedIndexChanged
			If txtVMFax.Enabled Then txtVMFax.Focus()
		End Sub

		Private Sub cboVMVDN_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboVMVDN.ClickEvent
			If eventArgs.area = 2 Then
				WriteToScreen()
			End If


		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddnew.Click
			ISADDING = True
			ISEDITING = True
			WriteToScreen()

			' Ham Gan tri man hinh
			ButtonStatus()

		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			' Gan tri man nhinh
			ISADDING = False
			ISEDITING = False

			ButtonStatus()
			txtVMVDN.Text = cboVMVDN.BoundText
			WriteToScreen()


		End Sub

		Private Sub cmdDelete_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDelete.Click
			Dim cmdExecute As Object
			Dim SQL As String
			SQL = "DELETE FROM AVM  WHERE VMVDN = '" & cboVMVDN.BoundText & "'"

			cmdExecute = New ADODB.Command
			cmdExecute.ActiveConnection = DBconn
			cmdExecute.CommandText = SQL
			cmdExecute.Execute()
			Adodc1.Refresh()


		End Sub

		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click

			' Ham gan tri man hinh
			ISADDING = False
			ISEDITING = True


			ButtonStatus()

		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click

			WriteToFile()
			Adodc1.Refresh()

			'''''''''''
			ISADDING = False
			ISEDITING = False

			ButtonStatus()


		End Sub

		Private Sub frmACP100_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			If Not InitSuccess Then
				BIENCHUNG()
			End If

			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc1.RecordSource = "AVM"	'SALEMAN
			Adodc1.Refresh()

			Adodc2.ConnectionString = ConnectString
			Adodc2.ConnectionTimeout = 10
			Adodc2.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc2.RecordSource = "APT"	'Term Of Payment
			Adodc2.Refresh()


			ISADDING = False
			ISEDITING = False
			ButtonStatus()
			VB6_AddADODataBinding()
			cboVMVDN.RowSource = ADOBind_Adodc1.DataSource
			cboVMTPM.RowSource = ADOBind_Adodc2.DataSource
		End Sub

		Private Sub txtVMADD_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtVMADD.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtVMTEL.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtVMFax_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtVMFax.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtVMTPM.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtVMNME_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtVMNME.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtVMADD.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtVMTEL_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtVMTEL.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				cboVMTYP.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtVMTPM_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtVMTPM.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim txtCMPTC As Object
			Dim Result As String
			If KeyAscii = 13 Then

				Result = Find("APT", "PTDSC", " WHERE PTPTC='" & txtCMPTC & "'")
				If Result <> "" Then
					txtPTDSC.Text = Result
					cboVMDLV.Focus()
				Else
					cboVMTPM.Visible = True
					cboVMTPM.Focus()
				End If



			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtVMVDN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtVMVDN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				If ValidMaso(OldVMVDN, txtVMVDN, "AVM", " VMVDN='" & txtVMVDN.Text & "'") Then
					MsgBox("This code is already exist")
					txtVMVDN.Focus()
				Else
					txtVMNME.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
	End Class
End Namespace